<template>
  <el-dialog title="附件列表" width="900px" :close-on-click-modal="false">
    <el-row>
      <template v-for="(item, key) in props.annexList" :key="key">
        <el-col
          style="margin-bottom: 20px"
          :span="4"
          :offset="key % 5 === 0 ? 0 : 1"
        >
          <el-card
            :body-style="{ padding: '0px' }"
            style="text-align: center; padding-top: 10px; height: 155px"
          >
            <span
              class="iconfont"
              style="font-size: 80px; cursor: pointer"
              :class="setIcon(item.name)"
              @click="previewFile(item.path)"
            ></span>
            <div style="padding: 5px">
              <div>{{ item.name }}</div>
            </div>
            <div>
              <el-button type="text" icon="download">下载</el-button>
              <el-button type="text" icon="delete" @click="deleteFile(item)"
                >删除</el-button
              >
            </div>
          </el-card>
        </el-col>
      </template>
      <el-col :span="4" :offset="props.annexList.length % 5 === 0 ? 0 : 1">
        <el-card
          :body-style="{ padding: '0px' }"
          style="text-align: center; padding-top: 10px; height: 155px"
          shadow="hover"
        >
          <el-upload
            :show-file-list="false"
            :action="
              '/api/annex/' + props.tableName + '/' + props.rowId + '/upload'
            "
            :on-success="successUpload"
          >
            <el-icon :size="80">
              <plus />
            </el-icon>
            <div style="padding: 5px">
              <p>添加附件</p>
            </div>
            <p>(请上传pdf文件)</p>
          </el-upload>
        </el-card>
      </el-col>
    </el-row>
    <template #footer>
      <div style="float: left">
        <el-button type="primary" v-show="true">上一条</el-button>
        <el-button type="primary" v-show="true">下一条</el-button>
      </div>
      <el-button @click="changeAnnexDialogVisible">关闭 </el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { inject } from "vue";
import { Annex } from "@/models/Annex";
import api from "@/utils";

export interface annexDialogProps {
  tableName: string;
  rowId: number;
  annexList: Annex[];
}

const props = defineProps<annexDialogProps>();

const changeAnnexDialogVisible = inject(
  "changeAnnexDialogVisible"
) as () => void;

const getAnnexList = inject("getAnnexList") as (
  tableName: string,
  rowId: number
) => void;

const successUpload = (
  response: Response,
  file: Record<string, never>,
  fileList: File[]
) => {
  console.log(response);
  console.log(file);
  console.log(fileList);
  getAnnexList(props.tableName, props.rowId);
};

const setIcon = (itemName: string) => {
  let fileSuffix = getFileSuffix(itemName);
  switch (fileSuffix) {
    case ".pdf":
      return "icon-pdf";
    case ".xls":
    case ".xlsx":
      return "icon-Excel";
    case ".doc":
    case ".docx":
      return "icon-DOC";
    case ".jpg":
    case ".jpeg":
    case ".png":
      return "icon-24gl-pictures";
    default:
      return "icon-document";
  }
};

const previewFile = (filePath: string) => {
  let fileSuffix = getFileSuffix(filePath);
  switch (fileSuffix) {
    case ".pdf":
      window.open("/pdf/test?file=" + filePath, "_blank");
      return "icon-pdf";
    case ".xls":
    case ".xlsx":
      return "icon-Excel";
    case ".doc":
    case ".docx":
      return "icon-DOC";
    case ".jpg":
    case ".jpeg":
    case ".png":
      return "icon-24gl-pictures";
    default:
      return "icon-document";
  }
};

const getFileSuffix = (fileName: string) => {
  return fileName.substring(fileName.lastIndexOf("."), fileName.length);
};

const deleteFile = (item: Annex) => {
  api.http
    .postJson("/annex/" + props.tableName + "/" + item.id, item)
    .then((res) => {
      console.log(res);
      getAnnexList(props.tableName, item.rowId);
    });
};
</script>

<style scoped></style>
